import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-07-22
 * Time: 21:50
 * To BE A Better Person-2020-3-2
 */
public class TestDemo {
    //给定一个很长的数组 (长度 1000w), 通过随机数的方式生成 1-100 之间的整数.
    //实现代码, 能够创建两个线程, 对这个数组的所有元素求和.
    //其中线程1 计算偶数下标元素的和, 线程2 计算奇数下标元素的和.
    //最终再汇总两个和, 进行相加
    //记录程序的执行时间.
    static int sum2 = 0;
    static int sum1 = 0;
    public static boolean Index(int index){
        return (index%2)==1;
    }
    public static void main(String[] args) throws InterruptedException {
        int[] arr=new int[10000000];
        Random r=new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i]=r.nextInt(101);
        }
        int sum=0;
        Thread t1= new Thread(()->{
            for (int i = 0; i < arr.length; i++) {
                if(!Index(i)){
                    sum1+=arr[i];
                }

            }
        });
        Thread t2=new Thread(()->{
            for (int i = 0; i < arr.length; i++) {
                if(Index(i)){
                    sum2+=arr[i];
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        sum+=sum1+sum2;
        System.out.println(sum);
    }

}
